package com.kou.backend.resposne;

import lombok.Data;

import java.util.UUID;

/**
 * 统一结果返回
 *
 * @author: Kou
 * @since: 2023/8/24 20:50
 */
@Data
public class ResultResponse<T> {
    private static final String SUCCESS_CODE = "0";
    private static final String SUCCESS_MSG = "success";
    /**
     * req id
     */
    private String requestId;
    /**
     * 返回code
     */
    private String code;
    /**
     * 接口返回信息
     */
    private String msg;
    /**
     * 接口返回详细数据
     */
    private T data;

    public ResultResponse() {
        this.requestId = UUID.randomUUID().toString();
    }

    public ResultResponse(String requestId) {
        this.requestId = requestId;
    }

    public ResultResponse(T data) {
        this.data = data;
        this.msg = SUCCESS_MSG;
        this.code = SUCCESS_CODE;
    }

    public ResultResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultResponse<T> success(T data) {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(SUCCESS_CODE);
        resultResponse.setMsg(SUCCESS_MSG);
        resultResponse.setData(data);
        return resultResponse;
    }

    public static <T> ResultResponse<T> success() {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(SUCCESS_CODE);
        resultResponse.setMsg(SUCCESS_MSG);
        return resultResponse;
    }

    public static <T> ResultResponse<T> fail(String code, String msg) {
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(code);
        resultResponse.setMsg(msg);
        return resultResponse;
    }
}
